<?php
namespace app\index\controller;

use app\common\controller\Base;
use app\index\model\Ban as BanModel;
use app\index\model\DCharacter;
use app\index\model\Register;
use app\index\model\UserCharacter;

/**
 * 禁言封号
 */
class Ban extends Base
{

    public function _initialize()
    {
        //基类授权
        //
        parent::_initialize();
        $this->model = new BanModel;
        $this->character = new DCharacter;
    }
    /**
     * 账户查询
     */
    public function queryBasicInfo()
    {

        //角色ID昵称

        $_id       = input('id');
        $_nickname = input('nickname');

        $rows = [];
        $arr  = [];

        //ID搜索

        if (!empty($_id) || !empty($_nickname)) {

            //按照角色ID查询
            if (!empty($_id)) {

                //按照角色ID查询
                $arr = (!empty($_id)) ? explode(",", $_id) : null;

            } else if (!empty($_nickname)) {

                //按照昵称查询
                $arr_nickname = (!empty($_nickname)) ? explode(",", $_nickname) : null;

                foreach ($arr_nickname as $ncickname) {

                    $res = DCharacter::where("`nickname`='{$nickname}'")
                        ->field("`id`,`nickname`,`donttalk`")
                        ->find();

                    if ($res) {
                        $res = $res->getData('id');
                        array_push($arr, $res);
                    }
                }
            }

            //遍历查询对象
            foreach ($arr as $id) {

                $res = DCharacter::where("`id`='{$id}'")
                    ->field("`id`,`nickname`,`donttalk`")
                    ->find();

                if ($res) {

                    //查账号
                    $userCharacter = $res->userCharacter;
                    $uid           = $userCharacter->getData('uid');
                    $dCharacter    = Register::getById($uid);

                    //账号封禁
                    $res           = $res->getData();
                    $res['enable'] = $dCharacter->getData('enable');

                    if (1 == $res['donttalk']) {

                        //判断禁言截止日期是否比现在晚
                        $temp0 = BanModel::where("'{$id}'=`uid` AND UNIX_TIMESTAMP(`deadline`)>UNIX_TIMESTAMP(NOW()) AND 0=`operation`")
                            ->field("`uid`,`operation`,`deadline`,`reason`")
                            ->order("`deadline` desc")
                            ->limit(1)
                            ->select();

                        if ($temp0) {
                            $data                     = $temp0[0]->getData();
                            $res['donttalk_reason']   = $data['reason'];
                            $res['donttalk_deadline'] = $data['deadline'];
                        } else {

                            //取消禁言
                            $this->character->changeDontalkTo($id, 0);
                            $res['enable'] = 0;
                        }
                    }

                    if (0 == $res['enable']) {

                        //判断封号截止日期是否比现在晚
                        $temp1 = BanModel::where("'{$id}'=`uid` AND UNIX_TIMESTAMP(`deadline`)>UNIX_TIMESTAMP(NOW()) AND 1=`operation`")
                            ->field("`uid`,`operation`,`deadline`,`reason`")
                            ->order("`deadline` desc")
                            ->limit(1)
                            ->select();

                        if ($temp1) {
                            $data                    = $temp1[0]->getData();
                            $res['account_reason']   = $data['reason'];
                            $res['account_deadline'] = $data['deadline'];
                        } else {

                            //取消封号
                            $this->user->banAccount($id, 1);
                            $res['enable'] = 1;
                        }
                    }

                    array_push($rows, $res);
                }
            }
            //查询数据总计
            $total = count($rows);

            $rtn = array();

            if ($total > 0) {

                $rtn['rows']  = $rows;
                $rtn['total'] = $total;
                _callback($rtn);

            } else {

                $rtn['total'] = 0;
                $rtn['rows']  = array();
                _callback($rtn);
            }
        }
    }
    /**
     * 账户封禁
     */
    public function banAccount()
    {
        $ids      = json_decode(input('id'), true);
        $deadline = input('deadline');
        $reason   = input('reason');

        $this->model->banAccount($ids, $reason, $deadline);
    }

    /**
     * 账户解封
     */
    public function unbanAccount()
    {

        $ids = json_decode(input('id'), true);
        $this->model->unbanAccount($ids);
    }

    /**
     * 账户禁言
     */
    public function banComment()
    {

        $ids      = json_decode(input('id'), true);
        $deadline = input('deadline');
        $reason   = input('reason');

        $this->model->banComment($ids, $reason, $deadline);
    }

    /**
     * 禁言解封
     */
    public function unbanComment()
    {

        $ids = json_decode(input('id'), true);
        $this->model->unbanComment($ids);
    }

}
